package org.mogu.control;


import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.mogu.R;
import org.mogu.Welcome;
import org.mogu.staticvalue.Convert;
import org.mogu.tools.Imgtools;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Replay1 extends RelativeLayout {
	TextView title;
//	EditText edittext;
	Button cancelbtn;
//	Button replaybtn;
	Button sendbtn;
	Replay1 replay1;
	String code;
	String replayType;
	String maincode;
	String father;
	Welcome welcome;
	String check;
	String c;
	
	
	
	RadioGroup replay1group;

	DefaultHttpClient client;
	HttpResponse httpResponse;

	public Replay1(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public Replay1(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.replay1, this, true);
//		title = (TextView) findViewById(R.id.replay1title);
		
		replay1group=(RadioGroup)findViewById(R.id.replay1group);
		
//		edittext = (EditText) findViewById(R.id.weibo10text);
		cancelbtn = (Button) findViewById(R.id.weibo_cancel);
		cancelbtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				welcome.goBack();
			}
		});
		sendbtn = (Button) findViewById(R.id.weibo_ok);
		sendbtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				
				c="";
				if("1".equals(replayType)){
					if(replay1group.getCheckedRadioButtonId()==-1){
						Toast.makeText(welcome, "请选择一条选项后提交", 4000).show();
						return;
					}else{
						c+=replay1group.getCheckedRadioButtonId();
					}
				}else{
					CheckBox cbox;
					for(int i=0;i<replay1group.getChildCount();i++){
						cbox=(CheckBox)replay1group.getChildAt(i);
						if(cbox.isChecked()){
							c+=cbox.getId()+"*";
						}
					}
				}
				new Thread() {
					public void run() {
				try {
//					if(rbtng.getCheckedRadioButtonId()==R.id.radio1){
//						check="1";
//						
//					}
//					if(rbtng.getCheckedRadioButtonId()==R.id.radio2){
//						check="2";
//					}
//					if(rbtng.getCheckedRadioButtonId()==R.id.radio3){
//						check="3";
//					}
//					if(rbtng.getCheckedRadioButtonId()==R.id.radio4){
//						check="4";
//					}
//					if(rbtng.getCheckedRadioButtonId()==-1){
//						Toast.makeText(welcome, "请选择要开通的微博。", 4000).show();
//						return;
//					}
					
					
					String url;
					url = Convert.hosturl + "/AddReply?UserName="
							+ Convert.uname + "&UserPwd=" + Convert.upwd+"&Code="+code+"&MainCode="+maincode+"&Type="+replayType+"&Content="+c;
//					url="http://weibo.zxxsbook.com/login?username=11&website=wy";
					
					client = new DefaultHttpClient();
					
//					welcome.initDialog(null, null);
//					welcome.myDialog.show();
					HttpGet getMethod = new HttpGet(url+ Convert.testNum);

					httpResponse = client.execute(getMethod);

					if (httpResponse.getStatusLine().getStatusCode() == 200) {
						// 取出回应字串
						String strResult;
						strResult = EntityUtils.toString(httpResponse
								.getEntity());
						if ("1".equals(strResult)) {
//							Toast.makeText(welcome, "提交失败，稍后再试。", 4000).show();
						}
						if ("0".equals(strResult)) {
//							Toast.makeText(welcome, "提交成功。", 4000).show();
						}
					}else{
//						Toast.makeText(welcome, "提交失败，稍后再试。", 4000).show();
					}
					
				} catch (Exception e1) {
//					welcome.myDialog.dismiss();
				}finally{
//					welcome.goBack();
				}
					}}.start();
					welcome.goBack();
					Toast.makeText(welcome, "提交成功", 4000).show();
				
			}
		});
		
	}

	public void setContent(String code,String father, String replayType, String maincode,
			 String content) {
//		weibocontent.setText(content);
		replay1group.removeAllViews();
		this.code = code;
		this.replayType = replayType;
		this.maincode = maincode;
		this.father=father;
		int intId=1;
		RadioButton rbtn;
		CheckBox cbox;
		String[] w=content.split("\\*\\*");
		for(int i=0;i<w.length;i++){
			if(!"".equals(w[i])){
				if("1".equals(this.replayType)){
					rbtn=new RadioButton(replay1group.getContext());
					rbtn.setId(intId);
					rbtn.setText(Imgtools.getContent(w[i]));
					replay1group.addView(rbtn);
				}
				if("2".equals(this.replayType)){
					cbox=new CheckBox(replay1group.getContext());
					cbox.setId(intId);
					cbox.setText(Imgtools.getContent(w[i]));
					replay1group.addView(cbox);
				}
				intId++;
			}
		}
	}

	

	public void setReplay(Replay1 content, Welcome welcome) {
		replay1 = content;
		this.welcome = welcome;
	}

}
